package sanphone.com.runningschool;


import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import sanphone.com.runningschool.Fragment.SayingFragment;
import sanphone.com.runningschool.Fragment.JushouFragment;
import sanphone.com.runningschool.Fragment.SettingFragment;

public class MainActivity extends BaseActivity implements TabHost.TabContentFactory {

    private TabHost mTabHost;
    private ViewPager mViewPager;
    private SharedPreferences sprf;


    private String[] titles = new String[]{
            "举手之劳",
            "我有话说",
            "个人设置"

    };
    //三个Tab图标
    private int[] drawableIDs = {
            R.drawable.main_tab_icon_home,
            R.drawable.main_tab_icon_msg,
            R.drawable.main_tab_icon_my
    };
    //三个碎片
    private Fragment[] fragments = new Fragment[]{
            JushouFragment.newInstance(),
            SayingFragment.newInstance(),
            SettingFragment.newInstance()
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sprf=getSharedPreferences("data",MODE_PRIVATE);
        ForceOffline offline=new ForceOffline(sprf.getString("schoolId",null),this);
        offline.check();

        initView();
        bindTab();


// TODO: 2018/7/22 添加适配器
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
        // TODO: 2018/7/19 Tab标签和Fragment绑定
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mTabHost != null) {
                    mTabHost.setCurrentTab(position);

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setOffscreenPageLimit(3);
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                if (mTabHost != null) {
                    int position = mTabHost.getCurrentTab();
                    mViewPager.setCurrentItem(position);
                }
            }
        });

    }

    // TODO: 2018/7/22 初始化布局
    private void initView() {
        mTabHost = findViewById(R.id.tab_host);
        mTabHost.setup();
        mViewPager = findViewById(R.id.view_pager);



    }

    // TODO: 2018/7/22 data<-->view
    private void bindTab() {

        for (int index = 0; index < titles.length; index++) {
            View view = getLayoutInflater().inflate(R.layout.main_tab, null, false);
            ImageView tabIcon = view.findViewById(R.id.main_tab_icon);
            TextView tabText = view.findViewById(R.id.main_tab_text);
            tabIcon.setImageResource(drawableIDs[index]);
            tabText.setText(titles[index]);
            mTabHost.addTab(
                    mTabHost.newTabSpec(titles[index])
                            .setIndicator(view)
                            .setContent(this)
            );
        }
    }



    @Override


    public View createTabContent(String s) {
        View view = new View(this);
        view.setMinimumHeight(0);
        view.setMinimumWidth(0);
        return view;
    }


}
